package com.soft.mainsite.component.beetl.tag.category;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.beetl.core.GeneralVarTagBinding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.soft.mainsite.common.exception.CmsException;
import com.soft.mainsite.common.utils.CmsUtil;
import com.soft.mainsite.common.utils.StrUtil;
import com.soft.mainsite.module.web.cms.service.CategoryService;
import com.soft.mainsite.module.web.cms.service.SiteService;
import com.soft.mainsite.mybatis.model.TCmsCategory;
import com.soft.mainsite.mybatis.model.TCmsSite;

/**
 * Description:根据栏目别名获取栏目标签
 *
 * @author liu
 * @create 2019-04-22
 **/
@Service
@Scope("prototype")
public class CategoryAliasTag extends GeneralVarTagBinding {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SiteService siteService;

    @Value("${system.http.protocol}")
    private String httpProtocol;

    @Value("${system.http.host}")
    private String httpHost;

    @Value("${system.site.prefix}")
    private String sitePrefix;

    @Value("${system.site.subfix}")
    private String siteSubfix;

    @Override
    public void render() {
        HttpServletRequest request = (HttpServletRequest) ctx.getGlobal("request");
        Map result = Maps.newHashMap();
        String categoryAlias = (String) (this.getAttributeValue("categoryAlias"));
        Integer isParent = (this.getAttributeValue("isParent") instanceof String) ? Integer.parseInt((String) this.getAttributeValue("isParent")) : (Integer) this.getAttributeValue("isParent");
        TCmsCategory category = categoryService.findByAlias(categoryAlias);
        TCmsSite site = siteService.findById(category.getSiteId());
        String staticHtmlPath = ctx.getGlobal("staticHtmlPath") == null ? "" : (String) ctx.getGlobal("staticHtmlPath");
        if (CmsUtil.isNullOrEmpty(category)) {
            throw new CmsException("栏目[" + categoryAlias + "]不存在！");
        }
        if (isParent == 1 && category.getParentId() != 0) {
            category = categoryService.findById(category.getParentId());
        }
        result.put("categoryId", category.getCategoryId());
        result.put("categoryIcon", category.getCategoryIcon());
        result.put("categoryName", category.getCategoryName());
        result.put("categoryDescription", category.getCategoryDescription());
        result.put("content", category.getContent());
        result.put("url", !StrUtil.isBlank(category.getUrl()) ? category.getUrl() : request.getContextPath() + staticHtmlPath + "/" + site.getSiteKey() + "/" + category.getAlias() + siteSubfix);
        result.put("more", !StrUtil.isBlank(category.getUrl()) ? category.getUrl() : request.getContextPath() + staticHtmlPath + "/" + category.getAlias() + "/index_1" + siteSubfix);
        result.put("prototype", category);
        this.binds(result);
        this.doBodyRender();
    }
}
